package com.frinder.official.akka.actors;

import akka.actor.ActorRef;
import com.frinder.official.spring.MyBaseActor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Created by frinder on 2017/9/13.
 */
@Component
@Scope("prototype")
public class StoppingActor extends MyBaseActor {

    private ActorRef child;

    @Override
    public void preStart() throws Exception {
        super.preStart();
        child = getContext().actorOf(springExtension.props("scheduledActor"), "ScheduledActor");
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .matchEquals("interrupt-child", m -> {
                    log.debug("{}", m);
                    getContext().stop(child);
                })
                .matchEquals("done", m -> {
                    log.debug("{}", m);
                    getContext().stop(getSelf());
                })
                .build();
    }
}
